package com.gljx.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

public class AjaxUtil {

	public static final String AJAX_ACCEPT_CONTENT_TYPE = "text/html;type=ajax";
	
	public static final String AJAX_SOURCE_PARAM = "ajaxSource";
	
	public static boolean isAjaxRequestInternal(HttpServletRequest request) {
		String acceptHeader = request.getHeader("Accept");
		
		String header = request.getHeader("X-Requested-With");  
		boolean isAjax = "XMLHttpRequest".equals(header) ? true:false; 
		//application/json, text/javascript, */*; q=0.01
		//text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
		String ajaxParam = request.getParameter(AJAX_SOURCE_PARAM);
		if (isAjax||AJAX_ACCEPT_CONTENT_TYPE.equals(acceptHeader) || StringUtils.hasText(ajaxParam)) {
			return true;
		} else {
			return false;
		}
	}
	public static boolean isAjax(HttpServletRequest request){
        return  (request.getHeader("X-Requested-With") != null  && "XMLHttpRequest".equals( request.getHeader("X-Requested-With").toString())   ) ;
    }
}
